//获取应用实例
var app = getApp();

// 初始化数据
var init = {

  // 数据
  data: {
    picker1Value: 0,
    dateValue: '请选择出生日期',
    score1: false,//1打分
    score2: false,//1打分
    score3: false,//1打分
    score4: false,//1打分
    score5: false,//1打分
    score: '',//分数
    title: '',//标题
    image: '',//图片
    // 产品列表
    items: [],

    // 数据加载
    loading: true,

    // 加载提示
    loadTips: '',

    // 选择的图片
    selectImg: [],
    //最终上传的图片
    imgs: [],
    id: '',//农场ID
    sex: '',

  },

  // 页面加载
  onLoad: function (options) {
    this.setData({
      options: options,
      id: options.order_id,
      title: options.title,
      image: options.images,
      doctor_id:options.id,
    });
      if(options.name!=undefined){
        this.setData({
          tap:options.name
        })
      }
    // this.getData();
    var that = this;
    //获取医生列表
    wx.request({
      url: app.api,
      data: {
        app: 'doctor',
        act: 'index'
      },
      success: function (res) {
        var data = res.data;
        if (data.done && data.retval) {
          that.setData({
            doctor: data.retval[that.data.options.id]
          })
        }
        console.log(that.data.doctor);
      }
    })
    //普通用户个人接口
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'member',
          act: 'myDetail',
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          if (data.done && data.retval) {
            that.setData({
              count: data.retval.count,
              member: app.turn(data.retval.member),
              loading: false
            })
            console.log(that.data.member);
            if (that.data.member[0].is_doctor == 1) {
              that.setData({
                doctors: that.data.member[0].is_doctor
              })
            }
          }
        },
        fail: function (res) {
          that.setData({
            loading: true,
            errorTips: "app:menber连接失败"
          })
        }
      })
    })
  },

  // 用户输入
  getTextarea: function (event) {
    var txt = event.detail.value
    console.log(txt);
    this.setData({
      textarea: txt
    })
  },

  // 验证
  verify: function (fn) {
    var that = this;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var reg = /^[\u4E00-\u9FA5]{2,4}$/;
    console.log(that.data.phone);
    if (that.data.name == "") {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (!reg.test(that.data.name)) {
      wx.showToast({
        title: '请输入真实姓名',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (that.data.sex == "") {
      wx.showToast({
        title: '请选择性别',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (that.data.content == "") {
      wx.showToast({
        title: '请输入症状',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (that.data.dateValue == "请选择出生日期") {
      wx.showToast({
        title: '请选择出生日期',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    return true;
  },

  // 最终提交（确定发表）
  submit: function (res) {
    var that = this;
    // wx.redirectTo({
    //   url: '/pages/docpaysuccess/docpaysuccess',
    // })
    if (that.verify()) {
      if (that.data.selectImg.length > 0) { // 有图片先上传图片
        for (var i = 0; i < that.data.selectImg.length; i++) {

          if (i == that.data.selectImg.length - 1) {
            that.uploadImg(that.data.selectImg[i], function () {
              that.submitData();
            })
          } else {
            that.uploadImg(that.data.selectImg[i], function () {
            })
            console.log(that.data.img);
          }
        }
      } else { // 没图片
        that.submitData();
      }
    }
  },
  // 提交问诊信息
  submitData: function () {

    var that = this;
    var that = this;
    if (that.data.doctors == 1) {
      wx.showToast({
        title: '您不能问诊',
        icon: 'loading'
      })
    } else {
    console.log(that.data.imgs);
    app.getLoginSession(function (session) {
      console.log(that.data.options);
      wx.request({
        url: app.api,
        data: {
          app: 'consult',
          act: 'addUserConsult',
          id: that.data.doctor_id,
          name: that.data.name,
          sex: that.data.sex,
          birth: that.data.dateValue,
          content: that.data.content,
          head_img: that.data.imgs,
          t_session: session
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log("成功");
          var data = res.data;
          console.log(data);

          if (data.done && data.retval) {
            // wx.showToast({
            //   title: data.retval.msg
            // });
            // wx.navigateTo({
            //   url: '/pages/evalSuccess/evalSuccess',
            // })
            wx.showModal({
              title: '提示',
              content: data.retval.msg,
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/pay/pay?consult_id=' + data.retval.consult_id + '&&id=' + that.data.doctor_id,
                  })
                } else {
                  wx.navigateTo({
                    url: '/pages/pay/pay?consult_id=' + data.retval.consult_id + '&&id=' + that.data.doctor_id,
                  })
                }
              }
            })
          } else {
            wx.showToast({
              title: '提交失败'
            })
          }
        }, fail: function () {
          console.log('shibai');
        }
      })
    })
    }
  },

  // 上传图片
  uploadImg: function (img, fn) {
    var that = this;
    var imgs = that.data.imgs;
    app.getLoginSession(function (session) {
      wx.uploadFile({
        url: app.api,
        filePath: img,
        name: 'file',
        formData: {
          app: 'member',
          act: 'upload_file',
          file: img,
          t_session: session
        },
        method: 'POST',
        header: {
          'content-type': 'multipart/form-data'
        },
        success: function (res) {
          var data = JSON.parse(res.data);
          var rlt = data.retval;
          if (data.done) {

            imgs.push(rlt.save_path);
            that.setData({
              imgs: imgs
            })
            console.log("sss" + that.data.imgs)
            typeof fn == 'function' && fn();
            console.log(fn)

          } else {
            console.log(data);
            that.tips('图片上传失败')
          }
          console.log(res);
        },
        fail: function () {
          console.log("失败");
        }
      })
    })
  },

  // 提示框
  tips: function (txt) {
    wx.showModal({
      title: '提示',
      content: txt,
      showCancel: false
    })
  },

  // 选择图片
  chooseImg: function () {
    var that = this;
    var selectImg = that.data.selectImg;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {

        var tempFilePaths = res.tempFilePaths

        if (tempFilePaths.length + selectImg.length > 5) {
          that.tips('最多只能上传5张')
        } else {
          for (var i = 0; i < tempFilePaths.length; i++) {
            selectImg.push(tempFilePaths[i])
            // that.uploadImg(tempFilePaths[i])
          }
          that.setData({
            selectImg: selectImg
          })
          console.log(selectImg);
        }

      },
      fail: function () {
        that.tips('选取图片失败')
      }
    })
  },
  // 删除图片
  deleteImg: function (event) {
    var that = this;
    var selectImg = that.data.selectImg;
    var url = event.currentTarget.dataset.url;
    for (var i in selectImg) {
      if (selectImg[i] == url) {
        selectImg.splice(i, 1);
        break
      }
    }
    that.setData({
      selectImg: selectImg
    });
    console.log(that.data.selectImg);
  },
  scoreone: function () {
    var that = this;
    that.setData({
      score1: true,
      score2: false,
      score3: false,
      score4: false,
      score5: false,
      score: 1
    })
  },
  scoretwo: function () {
    var that = this;
    that.setData({
      score1: true,
      score2: true,
      score3: false,
      score4: false,
      score5: false,
      score: 2
    })
  },
  scorethree: function () {
    var that = this;
    that.setData({
      score1: true,
      score2: true,
      score3: true,
      score4: false,
      score5: false,
      score: 3
    })
  },
  scorefour: function () {
    var that = this;
    that.setData({
      score1: true,
      score2: true,
      score3: true,
      score4: true,
      score5: false,
      score: 4
    })
  },
  scorefive: function () {
    var that = this;
    that.setData({
      score1: true,
      score2: true,
      score3: true,
      score4: true,
      score5: true,
      score: 5
    })
  },
  onShow: function () {
    // // 页面显示
    // var vm = this;
    // var length = vm.data.text.length * vm.data.size;//文字长度
    // var windowWidth = wx.getSystemInfoSync().windowWidth;// 屏幕宽度
    // vm.setData({
    //   length: length,
    //   windowWidth: windowWidth,
    //   marquee2_margin: length < windowWidth ? windowWidth - length : vm.data.marquee2_margin//当文字长度小于屏幕长度时，需要增加补白
    // });
    // vm.run2();// 第一个字消失后立即从右边出现
  },
  run2: function () {
    // var vm = this;
    // var interval = setInterval(function () {
    //   if (-vm.data.marqueeDistance2 < vm.data.length) {
    //     // 如果文字滚动到出现marquee2_margin=30px的白边，就接着显示
    //     vm.setData({
    //       marqueeDistance2: vm.data.marqueeDistance2 - vm.data.marqueePace,
    //       marquee2copy_status: vm.data.length + vm.data.marqueeDistance2 <= vm.data.windowWidth + vm.data.marquee2_margin,
    //     });
    //   } else {
    //     if (-vm.data.marqueeDistance2 >= vm.data.marquee2_margin) { // 当第二条文字滚动到最左边时
    //       vm.setData({
    //         marqueeDistance2: vm.data.marquee2_margin // 直接重新滚动
    //       });
    //       clearInterval(interval);
    //       vm.run2();
    //     } else {
    //       clearInterval(interval);
    //       vm.setData({
    //         marqueeDistance2: -vm.data.windowWidth
    //       });
    //       vm.run2();
    //     }
    //   }
    // }, vm.data.interval);
  },
  //医生显示与隐藏
  showdoc: function () {
    var that = this;
    if (that.data.doc == true) {
      that.setData({
        doc: false
      })
    } else {
      that.setData({
        doc: true
      })
    }
  },
  //孩子信息显示与隐藏
  showchi: function () {
    var that = this;
    if (that.data.chi == true) {
      that.setData({
        chi: false
      })
    } else {
      that.setData({
        chi: true
      })
    }
  },
  // 医生列表的列表
  show_docdetail: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    if (that.data.doc_id != id) {
      that.setData({
        doc_id: id,
      })
    } else {
      that.setData({
        doc_id: '',
      })
    }
  },
  // 性别
  sex: function (event) {
    var that = this;
    var sex = event.target.dataset.sex;
    console.log(sex);
    that.setData({
      sex: sex,
      tap2: event.target.dataset.name
    })
  },
  name: function (event) {
    var that = this;
    that.setData({
      name: event.detail.value
    })
  },
  datePickerBindchange: function (e) {
    var a = e.detail.value.split("-");

    this.setData({
      dateValue: a[0] + "." + a[1] + "." + a[2]
    })
    console.log(a);
  },
  content: function (event) {
    var that = this;
    that.setData({
      content: event.detail.value
    })
  }
};

// 注册页面
Page(init);

